import logging

from langchain_core.messages import HumanMessage

from graph import init_app_withmcp

logging.basicConfig(level=logging.INFO)


async def main():
    from contextlib import AsyncExitStack

    s = AsyncExitStack()
    app = await init_app_withmcp(s)

    while True:
        if (user_input := input("[user]:")).lower() == "#exit":
            break

        async for event in app.astream_events(
            {"messages": [HumanMessage(user_input)]}, config={"configurable": {"thread_id": "114514"}}
        ):
            if event["event"] == "on_chat_model_stream":
                print(event["data"]["chunk"].content, end="", flush=True)

        print("\n\n")

    await s.aclose()


if __name__ == "__main__":
    import asyncio

    asyncio.run(main())
